package com.gxks.mall_manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxks.commons.entity.Manager;
import com.gxks.commons.entity.ResponseData;
import com.gxks.mall_manager.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @PutMapping("/manager/editState/{state}")
    public ResponseData editState(@PathVariable Integer state ,
                                  @RequestBody Long[] managerId){
        int count = managerService.editState(managerId , state);
        return new ResponseData(200, "状态修改成功！",null);
    }

    @PostMapping("/manager/delsome")
    public ResponseData delSome(@RequestBody Long[] managerId){
        int count = managerService.deleteSome(managerId);
        return new ResponseData(200 , "delete ok !" , null);
    }

    @PutMapping("/manager")
    public ResponseData<Manager> editSave(@RequestBody Manager manager){
        int count = managerService.editSave(manager);
        return new ResponseData<>(200 , "edit ok !" , manager);
    }

    @PostMapping("/manager")
    public ResponseData<Manager> addSave(@RequestBody Manager manager){
        int count = managerService.addSave(manager);
        return new ResponseData<>(200,"add ok ! " ,manager);
    }

    @PutMapping("/resetState")
    public void resetState(@RequestBody Long id){
        int count = managerService.resetState(id);
    }

    @DeleteMapping("/manager/{id}")
    public ResponseData deleteById(@PathVariable Long id){
        int count = managerService.deleteById(id);
        return new ResponseData(200 , "delete ok !" ,id);
    }

    @GetMapping("/manager/{id}")
    public ResponseData<Manager> getManagerById(@PathVariable Long id){
        Manager manager = managerService.selectById(id);
        return new ResponseData<>(200 , "query ok !" ,manager);
    }

    @GetMapping("/manager")
    public ResponseData<PageInfo<Manager>> managerList(
            @RequestParam(required = false) Long manager_state ,
            @RequestParam(value = "keywords",defaultValue = "") String keywords,
            @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize" , defaultValue = "3") Integer pageSize){
        Map<String,Object> search = new HashMap<>();
        if(manager_state != null){
            search.put("manager_state" , manager_state);
        }
        search.put("keywords",keywords);
        PageHelper.startPage(pageNum,pageSize);
        List<Manager> list = managerService.selectAll(search);
        PageInfo<Manager> pageInfo = new PageInfo<>(list);
        return new ResponseData<>(200 , "ok !" ,pageInfo);
    }
}
